import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import legacy from '@vitejs/plugin-legacy'
import { createHtmlPlugin } from 'vite-plugin-html'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import DefineOptions from 'unplugin-vue-define-options/vite'
import type { RegisterPluginsParams, VitePlugins } from '../type'
import registerDevPlugins from './dev'
import registerProdPlugins from './prod'
import { defineConfig } from 'vite'
import ElementPlus from 'unplugin-element-plus/vite'

/**
 *  注册插件
 * @param options
 * @returns
 */
export default function registerPlugins(options: RegisterPluginsParams): VitePlugins {
  const { env, pathResolve, isEnvProduction } = options
  // vite插件
  let plugins: VitePlugins = [
    vue(),
    legacy({
      targets: ['defaults', 'ie >= 11', 'chrome 52'], //需要兼容的目标列表，可以设置多个
      additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
      renderLegacyChunks: true,
      polyfills: [
        'es.symbol',
        'es.array.filter',
        'es.promise',
        'es.promise.finally',
        'es/map',
        'es/set',
        'es.array.for-each',
        'es.object.define-properties',
        'es.object.define-property',
        'es.object.get-own-property-descriptor',
        'es.object.get-own-property-descriptors',
        'es.object.keys',
        'es.object.to-string',
        'web.dom-collections.for-each',
        'esnext.global-this',
        'esnext.string.match-all'
      ]
    }),
    // defineOptions 宏
    DefineOptions(),
    createHtmlPlugin({
      inject: {
        data: { ...env }
      },
      minify: true
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [pathResolve('src/icons')],
      // 指定symbolId格式
      symbolId: 'icon-[name]'
    }),
    vueJsx(),
    Components({
      resolvers: [VantResolver()],
      dts: false,
      directoryAsNamespace: true
    })
  ]

  plugins = isEnvProduction
    ? [...plugins, ...registerProdPlugins(options)]
    : [...plugins, ...registerDevPlugins(options)]

  return plugins
}
